package com.kaia.shoebox.activity;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.googlecode.android.widgets.DateSlider.DateSlider;
import com.googlecode.android.widgets.DateSlider.DateTimeSlider;
import com.googlecode.android.widgets.DateSlider.labeler.TimeLabeler;
import com.kaia.shoebox.R;
import com.kaia.shoebox.db.dao.Importance;

public class EditTodoActivity extends Activity {
	private static final String TAG = EditTodoActivity.class.getSimpleName();

	// ------------------------------------------------------------------
	// Attributes
	// ------------------------------------------------------------------

	private static final int DATE_TIME_DIALOG = 0;

	// ------------------------------------------------------------------
	// Attributes
	// ------------------------------------------------------------------

	private EditText mTodoTitle;
	private TextView mDueDateText;
	private Button mShowDatePicker;
	private EditText mAdditionalStuff;
	private Spinner mImportanceSpinner;
	private Button mSaveChangesButton;

	private DateSlider.OnDateSetListener mDateSetListener;

	// ------------------------------------------------------------------
	// System Methods
	// ------------------------------------------------------------------

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.edit_todo);

		Intent data = getIntent();
		String title = data.getStringExtra("title");
		long due = data.getLongExtra("due", 0);
		String notes = data.getStringExtra("notes");
		int imp = data.getIntExtra("importance", 0);
		final int id = data.getIntExtra("_id", 0);

		Date dueDate = new Date(due);

		mTodoTitle = (EditText) findViewById(R.id.edit_todo_title);
		mTodoTitle.setText(title);

		mDueDateText = (TextView) findViewById(R.id.edit_todo_due_date);

		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm");

		mDueDateText.setText(sdf.format(dueDate));

		mShowDatePicker = (Button) findViewById(R.id.edit_todo_due_date_button);
		mShowDatePicker.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				showDialog(DATE_TIME_DIALOG);
			}
		});

		mDateSetListener = new DateSlider.OnDateSetListener() {

			@Override
			public void onDateSet(DateSlider view, Calendar selectedDate) {
				int minute = selectedDate.get(Calendar.MINUTE)
						/ TimeLabeler.MINUTEINTERVAL
						* TimeLabeler.MINUTEINTERVAL;
				mDueDateText
						.setText(selectedDate.get(Calendar.DAY_OF_MONTH) + "/"
								+ selectedDate.get(Calendar.MONTH) + "/"
								+ selectedDate.get(Calendar.YEAR) + " "
								+ selectedDate.get(Calendar.HOUR_OF_DAY) + ":"
								+ minute);

			}
		};

		mAdditionalStuff = (EditText) findViewById(R.id.edit_todo_extra_info);
		mAdditionalStuff.setText(notes);

		mImportanceSpinner = (Spinner) findViewById(R.id.edit_todo_importance_spinner);
		mImportanceSpinner
				.setOnItemSelectedListener(new OnItemSelectedListener() {

					public void onItemSelected(AdapterView<?> parent,
							View view, int pos, long id) {
						// Do Nothing
					}

					public void onNothingSelected(AdapterView<?> parent) {
						// Do Nothing
					}
				});
		mImportanceSpinner.setSelection(imp);

		mSaveChangesButton = (Button) findViewById(R.id.edit_todo_edit_button);
		mSaveChangesButton.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				try {

					String title = mTodoTitle.getText().toString();

					String strDueDate = mDueDateText.getText().toString();

					SimpleDateFormat sdf = new SimpleDateFormat(
							"dd/MM/yyyy HH:mm");
					Date dueDate = sdf.parse(strDueDate);

					String notes = mAdditionalStuff.getText().toString();

					Importance importance = Importance
							.getImprtanceByNumber(mImportanceSpinner
									.getSelectedItemPosition());

					if (title.equals("")) {
						Toast.makeText(EditTodoActivity.this,
								"Title, empty, please enter one",
								Toast.LENGTH_SHORT).show();
					} else {
						Intent data = new Intent();
						data.putExtra("_id", id);
						data.putExtra("title", title);
						data.putExtra("due", dueDate.getTime());
						data.putExtra("notes", notes);
						data.putExtra("importance",
								importance.getImportanceNumber());

						Log.i(TAG,
								"Sending result: NEW_TODO:[title=" + title
										+ ", due=" + dueDate.getTime()
										+ ", notes=" + notes + ", importance="
										+ importance.getImportanceNumber());

						setResult(RESULT_OK, data);
						finish();
					}
				} catch (ParseException e) {
					Log.e(TAG, "Date parsing error", e);
				}
			}
		});

	}

	@Override
	protected Dialog onCreateDialog(int id) {
		final Calendar c = Calendar.getInstance();
		switch (id) {
		case DATE_TIME_DIALOG:
			return new DateTimeSlider(this, mDateSetListener, c);
		default:
			return super.onCreateDialog(id);
		}
	}
}
